☰ See All Chapters |
ofNullable in Java 9 Stream
ofNullable() method of stream takes an object as an argument. If the provided object is non-null, returns a sequential Stream containing that single object, if the value passed to ofNullable() method is null then it returns an empty Stream.
Java 9 Stream ofNullable Method Example
package com.java4coding.test;
import java.util.stream.Collectors; import java.util.stream.Stream;
public class Test { public static void main(String[] args) { Integer i1 = 1; Stream<Integer> stream1 = Stream.ofNullable(i1); System.out.println(stream1.collect(Collectors.toList()));
Integer i2 = null; Stream<Integer> stream2 = Stream.ofNullable(i2); System.out.println(stream2.collect(Collectors.toList()));
} } |
Output:
[1]
[]
All Chapters